package com.qtt.app.service.impl;

import com.qtt.app.entity.*;
import com.qtt.app.entity.ReturnInfo.ReturnCoupon;
import com.qtt.app.mapper.*;
import com.qtt.app.service.AppCouponService;
import com.qtt.app.utils.DateUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.qtt.app.utils.Converter.getAsInteger;
import static com.qtt.app.utils.DateUtil.getNowTime_EN;
import static com.qtt.app.utils.DateUtil.isInTimeRange;
import static com.qtt.app.utils.RandomUtils.getCouponRandom;

@Service
public class AppCouponServiceImpl implements AppCouponService {
    @Autowired
    AppCouponMapper appCouponMapper;
    @Autowired
    AppMyCouponMapper appMyCouponMapper;
    @Autowired
    GeneralCourseMapper generalCourseMapper;
    @Autowired
    AppInstitutionMapper appInstitutionMapper;
    @Autowired
    AppCourseMapper appCourseMapper;

    @Override
    public List<ReturnCoupon> getCouponByUserId(Long userId,int flag) {

        List<AppMyCoupon> appMyCouponEntities = appMyCouponMapper.getCouponByUserId(userId);
        List<ReturnCoupon> returnCouponEntities = new ArrayList<ReturnCoupon>();
        if(appMyCouponEntities.size() == 0){return null;}
        for(int i=0;i < appMyCouponEntities.size();i++){
            AppMyCoupon appMyCouponEntity = appMyCouponEntities.get(i);//获取当前的用户优惠券记录
            System.out.println("appMyCouponEntity=");
            System.out.println(appMyCouponEntity);
            //设置单个的返回体记录
            ReturnCoupon returnCouponEntity = new ReturnCoupon();
            returnCouponEntity.setCouponId(appMyCouponEntity.getCouponId());
            returnCouponEntity.setUserId(userId);
            returnCouponEntity.setIsUsed(appMyCouponEntity.getIsUsed());//是否使用在mycoupon表获取字段
            //取出mycoupon表的couponId,去app_coupon表拿取相关的数据
            Integer couponId = appMyCouponEntity.getCouponId();
            AppCoupon appCouponEntity = appCouponMapper.selectByPrimaryKey(couponId);
            returnCouponEntity.setCouponName(appCouponEntity.getCouponName());
            returnCouponEntity.setCouponType(appCouponEntity.getCouponType());
            returnCouponEntity.setDiscount(appCouponEntity.getDiscount());
            returnCouponEntity.setDiscountType(appCouponEntity.getDiscountType());
            returnCouponEntity.setSuitCourse(appCouponEntity.getSuitCourse());
            returnCouponEntity.setTimeRange(appCouponEntity.getTimeRange());
            returnCouponEntity.setCouponNumber(appCouponEntity.getCouponNumber());
            //判断是否是单课程优惠券，是的话需要通过课程genenralId去课程表中拿取课程名
            String couponType = appCouponEntity.getCouponType();
            if(couponType.equals("单课程优惠券")){
                String generalId = appCouponEntity.getSuitCourse();
                GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(generalId);
                if(generalCourseEntity == null){returnCouponEntity.setCourseName(null);}
                else{
                    returnCouponEntity.setCourseName(generalCourseEntity.getTitle());
                }
            }
            if(couponType.equals("机构课程优惠券")){
                Integer institutionId = getAsInteger(appCouponEntity.getSuitCourse());
                AppInstitution institutionEntity = appInstitutionMapper.selectByPrimaryKey(institutionId);
                if(institutionEntity == null){returnCouponEntity.setInstitutionName(null);}
                else{
                    returnCouponEntity.setInstitutionName(institutionEntity.getInstitutionName());
                }
            }

            //判断优惠券是否在有效期内
            System.out.println("现在时间是："+getNowTime_EN());
            String nowTime = getNowTime_EN();
            String timeRange = appCouponEntity.getTimeRange();
            String[] timeRange2= timeRange.split("~");
            System.out.println("优惠券开始时间是："+timeRange2[0]+";过期时间是："+timeRange2[1]);//选择第2个元素
            //比较两个日期
            boolean result=isInTimeRange(nowTime,timeRange2[0]+" 00:00:00",timeRange2[1]+" 00:00:00");//a早于b返回true，否则返回false
            System.out.println("是否在优惠券有效期内："+result);
            if(result==true){
                returnCouponEntity.setIsOverdue(true);
            }
            else{
                returnCouponEntity.setIsOverdue(false);
            }
            //添加到列表
            if(flag==1) {
                returnCouponEntities.add(returnCouponEntity);
            }
            if(flag==2 && result==true && appMyCouponEntity.getIsUsed()==0){
                returnCouponEntities.add(returnCouponEntity);
            }
            if(flag==3 && appMyCouponEntity.getIsUsed()==1){
                returnCouponEntities.add(returnCouponEntity);
            }
            if(flag==4 && result==false && appMyCouponEntity.getIsUsed()==0){
                returnCouponEntities.add(returnCouponEntity);
            }
        }
        return returnCouponEntities;
    }

    @Override
    public int addCoupon(String couponName, String timeRange, Float discount, String discountType, String couponType, String suitCourse) {
        AppCoupon appCouponEntity = new AppCoupon();
        appCouponEntity.setCouponName(couponName);
        appCouponEntity.setTimeRange(timeRange);
        appCouponEntity.setDiscount(discount);
        appCouponEntity.setDiscountType(discountType);
        appCouponEntity.setCouponType(couponType);
        appCouponEntity.setSuitCourse(suitCourse);
        appCouponEntity.setIsUsed(0);
        String couponNumber = getCouponRandom(5);
        appCouponEntity.setCouponNumber(couponNumber);
        int b = appCouponMapper.insert(appCouponEntity);
        if(b!=0){return 1;}
        else{return 0;}
    }

    @Override
    public int addMyCoupon(Long userId, Integer couponId,Integer isUsed) {
        AppMyCoupon appMyCouponEntity = new AppMyCoupon();
        appMyCouponEntity.setUserId(userId);
        appMyCouponEntity.setCouponId(couponId);
        appMyCouponEntity.setIsUsed(isUsed);
        int b = appMyCouponMapper.insert(appMyCouponEntity);
        if(b!=0){return 1;}
        else{return 0;}
    }

    @Override
    public int exchangeCoupon(Long userId, String couponNumber, Integer isUsed) {
        AppMyCoupon appMyCouponEntity = new AppMyCoupon();
        appMyCouponEntity.setUserId(userId);
        Integer couponId = appMyCouponMapper.getIdByCouponNumber(couponNumber);
        appMyCouponEntity.setCouponId(couponId);
        appMyCouponEntity.setIsUsed(isUsed);
        int b = appMyCouponMapper.insert(appMyCouponEntity);
        if(b!=0){return 1;}
        else{return 0;}
    }

    @Override
    public List<ReturnCoupon> getCourseCoupon(Long userId, String generalId) {
        List<ReturnCoupon> returnCouponEntities = appCouponMapper.getUserCoupon(userId);
        System.out.println(returnCouponEntities);
        List<ReturnCoupon> returnCouponEntities2 = new ArrayList<ReturnCoupon>();
        //以该课程id查找到该课程对应的课程类别和机构id，以便和之后的suitcourse对比
        String courseId =generalId+"1";
        String CourseType = appCourseMapper.getTypeByCourseId(courseId);
        Integer institutionId = appCourseMapper.getInstitutionIdByCourse(generalId);
        for(int i=0;i<returnCouponEntities.size();i++){
            String couponType = returnCouponEntities.get(i).getCouponType();
            String suitCourse = returnCouponEntities.get(i).getSuitCourse();

            //判断优惠券是否在有效期内
            System.out.println("现在时间是："+getNowTime_EN());
            String nowTime = getNowTime_EN();
            String timeRange = returnCouponEntities.get(i).getTimeRange();
            String[] timeRange2= timeRange.split("~");
            System.out.println("优惠券开始时间是："+timeRange2[0]+";过期时间是："+timeRange2[1]);//选择第2个元素
            //比较两个日期
            boolean result=isInTimeRange(nowTime,timeRange2[0]+" 00:00:00",timeRange2[1]+" 00:00:00");//a早于b返回true，否则返回false
            System.out.println("是否在优惠券有效期内："+result);
            if(result==true){
                returnCouponEntities.get(i).setIsOverdue(true);
            }
            else{
                returnCouponEntities.get(i).setIsOverdue(false);
            }

            //只有满足条件，才能添加
            if(couponType == "全课程优惠券" && returnCouponEntities.get(i).getIsUsed()==0){
                returnCouponEntities2.add(returnCouponEntities.get(i));
            }
            if(couponType == "单课程优惠券" && suitCourse.equals(generalId) && returnCouponEntities.get(i).getIsUsed()==0){
                GeneralCourse generalCourseEntity = generalCourseMapper.selectByPrimaryKey(generalId);
                if(generalCourseEntity == null){returnCouponEntities.get(i).setCourseName(null);}
                else{
                    returnCouponEntities.get(i).setCourseName(generalCourseEntity.getTitle());
                }
                returnCouponEntities2.add(returnCouponEntities.get(i));
            }
            if(couponType == "特定课别优惠券" && suitCourse.equals(CourseType) && returnCouponEntities.get(i).getIsUsed()==0){
                returnCouponEntities2.add(returnCouponEntities.get(i));
            }
            if(couponType == "机构课程优惠券" && suitCourse.equals(institutionId) && returnCouponEntities.get(i).getIsUsed()==0){
                AppInstitution institutionEntity = appInstitutionMapper.selectByPrimaryKey(institutionId);
                if(institutionEntity == null){returnCouponEntities.get(i).setInstitutionName(null);}
                else{
                    returnCouponEntities.get(i).setInstitutionName(institutionEntity.getInstitutionName());
                }
                returnCouponEntities2.add(returnCouponEntities.get(i));
            }

        }
        return returnCouponEntities;
    }
}
